package com.smallkingson.ylib.net.download;
import android.util.Pair;

import com.smallkingson.ylib.db.bean.KeyObtainable;
import com.smallkingson.ylib.db.cache.YCache;
public class DownloadFile implements YCache ,KeyObtainable {

	private String url;
	private String localPath;
	private String fileName;
	private long fileLength;
	private long finished;
	private long keepTime;
	private long cacheTime;
	public YDownloads.From from;

	public DownloadFile(String url) {
		super();
		this.url = url;
	}

	public DownloadFile() {

	}

	public String getUrl() {
		if (this.url == null) {
			throw new IllegalArgumentException("url is not init");
		}
		return url;
	}

	public DownloadFile setUrl(String url) {
		this.url = url;
		return this;
	}

	public String getFileName() {
		if (this.fileName == null) {
			fileName = url.substring(url.lastIndexOf("/") + 1);
		}
		return fileName;
	}

	public DownloadFile setFileName(String fileName) {
		this.fileName = fileName;
		return this;
	}

	public long getFileLength() {
		return fileLength;
	}

	public DownloadFile setFileLength(long length) {
		this.fileLength = length;
		return this;
	}

	public String getLocalPath() {
		return localPath;
	}

	public DownloadFile setLocalPath(String localPath) {
		this.localPath = localPath;
		return this;
	}

	public long getFinished() {
		return finished;
	}

	public void setFinished(long finished) {
		this.finished = finished;
	}

	@Override
	public String getCacheKey() {
		return getUrl();
	}

	@Override
	public long getKeepTime() {
		return this.keepTime;
	}

	@Override
	public long getCacheTime() {
		return this.cacheTime;
	}

	public DownloadFile setKeepTime(long keepTime) {
		this.keepTime = keepTime;
		return this;
	}

	public void setCacheTime(long cacheTime) {
		this.cacheTime = cacheTime;
	}
	@Override
	public Pair<String, Object>[] obtainKey() {
		return new Pair[]{
				new Pair<>("cacheKey", getCacheKey())
		};
	}
}